package com.ctgu.rabbitmq.rabbitmq.exchange.headersExchange;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ ClassName HeadersExchangeConfig
 * @ Author Alex2
 * @ Date 2025/8/14 15:26
 **/
@Configuration
public class HeadersExchangeConfig {

    public static final String HEADERS_EXCHANGE = "headers_exchange";
    public static final String QUEUE_HR = "queue_hr";
    public static final String QUEUE_IT = "queue_it";

    // 创建头部交换机
    @Bean
    public HeadersExchange headersExchange() {
        return new HeadersExchange(HEADERS_EXCHANGE);
    }

    // 创建队列
    @Bean
    public Queue queueHR() {
        return new Queue(QUEUE_HR, true);
    }

    @Bean
    public Queue queueIT() {
        return new Queue(QUEUE_IT, true);
    }

    // 绑定队列和交换机（根据 headers 匹配）
    @Bean
    public Binding bindingQueueHR(Queue queueHR, HeadersExchange headersExchange) {
        Map<String, Object> map = new HashMap<>();
        map.put("department", "HR");
        map.put("level", "senior");
        return BindingBuilder.bind(queueHR).to(headersExchange).whereAll(map).match();
    }

    @Bean
    public Binding bindingQueueIT(Queue queueIT, HeadersExchange headersExchange) {
        Map<String, Object> map = new HashMap<>();
        map.put("department", "IT");
        return BindingBuilder.bind(queueIT).to(headersExchange).whereAny(map).match();
    }
}